﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Company
{
    public partial class CompanyFavoriteList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindFavoriteCompanyList();
            }
        }

        #region 加载我收藏的小组列表
        private void BindFavoriteCompanyList()
        {
            Users uinfo = UserBase.GetCurrentUser();
            FavoriteHelper fhelper = new FavoriteHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            c.Add(CriteriaType.Equals, "ObjectType", 2);
            DataTable dt = fhelper.QueryFavoriteBySearchToTable(c, null, 0, 0, null);
            dt.Columns.Add(new DataColumn("CompanyOwnerID"));
            dt.Columns.Add(new DataColumn("CompanyOwnerName"));
            dt.Columns.Add(new DataColumn("CompanyName"));
            dt.Columns.Add(new DataColumn("BlogPVNum"));
            dt.Columns.Add(new DataColumn("UserNum"));
            dt.Columns.Add(new DataColumn("CareNum"));
            dt.Columns.Add(new DataColumn("RealmName"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string companyid = dr["objectid"] + "";
                    //加载小组信息
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(companyid);
                    dr["CompanyOwnerID"] = cinfo.OwnerUserID;
                    dr["CompanyOwnerName"] = cinfo.OwnerNickName;
                    dr["CompanyName"] = cinfo.CompanyName;
                    dr["BlogPVNum"] = cinfo.BlogPVNum.ToString();
                    dr["UserNum"] = cinfo.UserNum.ToString();
                    dr["CareNum"] = cinfo.CareNum.ToString();
                    dr["RealmName"] = cinfo.RealmName.ToString();
                }

                lblFavoriteCompany.Visible = false;
                rptFavoriteCompanyList.DataSource = dt;
                rptFavoriteCompanyList.DataBind();
            }
            else
            {
                lblFavoriteCompany.Visible = true;
                rptFavoriteCompanyList.Visible = false;
                lblFavoriteCompany.Text = "<font color=\"red\">暂时没有收藏的小组</font>";
            }

        }
        #endregion

        #region 收藏小组列表事件
        protected void rptFavoriteCompanyList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id = hidID.Value;
            if (e.CommandName == "delete")//取消收藏该小组
            {
                FavoriteHelper fhelper = new FavoriteHelper();
                Favorite finfo = new Favorite();
                finfo = fhelper.GetFavorite(id);
                fhelper.DeleteFavorite(finfo);

                Tools.AlertAndRedirect("删除收藏该小组成功", "CompanyFavoriteList.aspx");
            }
        }
        #endregion
    }
}